package pikes.peak;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.springframework.web.servlet.support.RequestContext;

import pikes.html.xhtml.Anchor;
import pikes.html.xhtml.Image;
import pikes.html.xhtml.Inline;
import pikes.peak.test.AssertHtml;
import pikes.peak.test.PikesPeakComponentTestCase;
import pikes.uri.URI;

public class ImageTestCase extends PikesPeakComponentTestCase {

	@Test
	public void simple() throws Exception {
		Picture picture = new Picture(new RequestProperty<URI>() {

			public URI getValue(Map model, RequestContext requestContext) throws Exception {
				return new URI() {

					public void print(Appendable appendable) throws IOException {
						appendable.append("testPath");
					}

				};
			}

		});

		Inline expected = new Image(new URI() {
			public void print(Appendable appendable) throws IOException {
				appendable.append("testPath");
			}
		}, "Missing description");
		Inline actual = picture.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void simpleWithDescription() throws Exception {
		Picture picture = new Picture(new RequestProperty<URI>() {

			public URI getValue(Map model, RequestContext requestContext) throws Exception {
				return new URI() {

					public void print(Appendable appendable) throws IOException {
						appendable.append("testPath");
					}

				};
			}

		}, "Some text");

		Inline expected = new Image(new URI() {
			public void print(Appendable appendable) throws IOException {
				appendable.append("testPath");
			}
		}, "Translated Text");
		Inline actual = picture.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void simpleNoURI() throws Exception {
		Picture picture = new Picture();

		Inline expected = new Image("#", "Missing description");
		Inline actual = picture.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void simpleNoURIWithDescription() throws Exception {
		Picture picture = new Picture("Some text");

		Inline expected = new Image("#", "Translated Text");
		Inline actual = picture.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void imageWithLink() throws Exception {
		Picture picture = new Picture(new RequestProperty<URI>() {

			public URI getValue(Map model, RequestContext requestContext) throws Exception {
				return new URI() {

					public void print(Appendable appendable) throws IOException {
						appendable.append("testPath");
					}

				};
			}

		}, "Some text");
		picture.setURI(new RequestProperty<URI>() {

			public URI getValue(Map model, RequestContext requestContext) throws Exception {
				return new URI() {

					public void print(Appendable appendable) throws IOException {
						appendable.append("domain/path");
					}

				};
			}

		});

		Inline expected = new Anchor(new URI() {

			public void print(Appendable appendable) throws IOException {
				appendable.append("domain/path");

			}

		}, new Image(new URI() {

			public void print(Appendable appendable) throws IOException {
				appendable.append("testPath");

			}

		}, "Translated Text"));
		Inline actual = picture.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}
}
